<?php

namespace app\bh\controller;

use app\bh\model\MenuModel;
use app\bh\model\RuleModel;
use app\bh\model\UserModel;
use think\Controller;
use think\Request;
use think\route\Rule;

class RuleController extends BaseAdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $rules = RuleModel::with('menu')->select()->toArray();
        return $this->SuccessReturn('获取成功', $rules);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $data = $request->post();
        //获取参数
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $action = array_key_exists('action', $data) ? $data['action'] : '';
        $method = array_key_exists('method', $data) ? $data['method'] : '';
        $mid = array_key_exists('mid', $data) ? $data['mid'] : '';
        //检查如果为空
        if ($name == '' || $action == '' || $method == "" || $mid == '') return $this->ErrorReturn(403, '缺少必要参数');
        //检查是否存在菜单
        $menu = MenuModel::find($mid);
        if (!$menu) return $this->ErrorReturn(400, '不存在菜单');
        //检查是否存在相同名称
        $isset_action = RuleModel::where('action', $action)
            ->where('method', $method)
            ->where('mid', $mid)
            ->find();
        if ($isset_action) return $this->ErrorReturn(400, '存在相同的路由');
        $name_action = RuleModel::where('name', $name)->where('mid', $mid)->find();
        if ($name_action) return $this->ErrorReturn(400, '存在相同的权限名称');

        //添加
        $rule_model = new  RuleModel();
        $rule_model->name = $name;
        $rule_model->action = $action;
        $rule_model->method = $method;
        $rule_model->mid = $mid;
        $res = $rule_model->save();
        if ($res) return $this->SuccessReturn('添加成功', $rule_model);
        return $this->ErrorReturn(400, '添加失败');

    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        //
        $data = $request->put();
        //获取参数
        $id = array_key_exists('id', $data) ? $data['id'] : '';
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $action = array_key_exists('action', $data) ? $data['action'] : '';
        $method = array_key_exists('method', $data) ? $data['method'] : '';
        $mid = array_key_exists('mid', $data) ? $data['mid'] : '';
        //检查如果为空
        if ($id == '') return $this->ErrorReturn(403, '缺少id');
        $rule_data = RuleModel::where('id', $id)->find();
        if (!$rule_data) return $this->ErrorReturn(400, '不存在权限');
        //检查是否存在菜单
        if ($mid !== '') {
            $menu = MenuModel::find($mid);
            if (!$menu) return $this->ErrorReturn(400, '不存在菜单');
        }

        //检查是否存在相同名称
        if ($action != '') {
            $actionsql = RuleModel::where('action', $action);
            if ($mid != '') {
                $actionsql->where('mid', $mid);
            }
            $isset_action = $actionsql->find();
            if ($isset_action && $isset_action->id != $id) {
                return $this->ErrorReturn(400, '存在相同的路由');
            } else {
                $rule_data->action = $action;
            }
        }
        if ($name != '') {
            $namesql = RuleModel::where('name', $name);
            if ($mid != '') {
                $namesql->where("mid", $mid);
            }
            $name_action = $namesql->find();
            if ($name_action && $name_action->id != $id) {
                return $this->ErrorReturn(400, '存在相同的权限名称');
            } else {
                $rule_data->name = $name;
            }
        }
        if ($method != '') $rule_data->method = $method;
        if ($mid != '') $rule_data->mid = $mid;
        $res = $rule_data->save();
        if ($res) return $this->SuccessReturn("修改成功");
        return $this->ErrorReturn(400, '修改失败');


    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id, Request $request)
    {
        //
        $id = $request->post('id');
        if ($id == 0) return $this->ErrorReturn(400, '权限id无效');
        //查询是否存在此用户
        $rule = RuleModel::where('id', $id)->find();
        if (!$rule) return $this->ErrorReturn(404, '找不到此权限');
        $rule->delete();
        return $this->SuccessReturn('删除成功');
    }
}
